<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增课程')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-course-add">
        <input name="coursePackageId" class="form-control" type="hidden" th:value="${coursePackageId}" required>
        <div class="form-group">
            <label class="col-sm-3 control-label">类型：</label>
            <div class="col-sm-8">
                <select id="typeName" name="type" class="form-control m-b"
                        th:with="type=${@dict.getType('course_package_type')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">时长：</label>
            <div class="col-sm-8">
                <input id="durations" name="durations" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">月龄 低限：</label>
            <div class="col-sm-8">
                <input name="ageLow" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">月龄 高限：</label>
            <div class="col-sm-8">
                <input name="ageHigh" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group " id="indexInPackage_1">
            <label class="col-sm-3 control-label">显示顺序：</label>
            <div class="col-sm-8">
                <input id="indexInPackage" name="indexInPackage" class="form-control"  value="1" type="text" >
            </div>
        </div>

        <div class="form-group " id="type_1_1">
            <label class="col-sm-3 control-label">周次：</label>
            <div class="col-sm-8">
                <select id="type_1_1_val" name="type_1_1_val" class="form-control m-b"
                        th:with="type=${@dict.getType('tc_week')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group " style="display:none;"  id="type_1_1_2">
            <label class="col-sm-3 control-label">周次：</label>
            <div class="col-sm-8">
                <!--                家庭-->
                <select id="type_1_1_2_val"  class="form-control m-b"
                        th:with="type=${@dict.getType('tc_family_week')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group "  style="display:none;" id="type_1_1_3">
            <label class="col-sm-3 control-label">周次：</label>
            <!--            群体-->
            <div class="col-sm-8">
                <select id="type_1_1_3_val"  class="form-control m-b"
                        th:with="type=${@dict.getType('tc_group_week')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group " id="type_2_1">
            <label class="col-sm-3 control-label">面向：</label>
            <div class="col-sm-8">
                <select id="type_2_1_val" name="type_2_1_val" class="form-control m-b"
                        th:with="type=${@dict.getType('tc_face')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>

        <div class="form-group "  style="display:none;" id="type_2_2">
            <label class="col-sm-3 control-label">面向：</label>
            <div class="col-sm-8">
                <select id="type_2_2_val"  class="form-control m-b"
                        th:with="type=${@dict.getType('tc_family_face')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>



        <div class="form-group " id="type_1_2">
            <label class="col-sm-3 control-label">教材：</label>
            <div class="col-sm-8">
                <input id="type_1_2_val" class="form-control" type="text">
            </div>
        </div>


        <div class="form-group hidden">
            <label class="col-sm-3 control-label is-required">详情：</label>
            <div class="col-sm-8">
                <input id="details" name="details" class="form-control" type="text" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label is-required">标题：</label>
            <div class="col-sm-8">
                <input name="title" class="form-control" type="text" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <input name="comment" class="form-control" type="text">
            </div>
        </div>

    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: summernote-js"/>
<script th:inline="javascript">
    var prefix = ctx + "system/course"
    $("#form-course-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var type = $("#typeName").val();
            var details = "";
            //机构端
            if (type == 0) {
                details = "[{\"label\":\"周次\",\"value\":\"" + $("#type_1_1_val").val() + "\"}," +
                    "{\"label\":\"教材\",\"value\":\"" + $("#type_1_2_val").val() + "\"}," +
                    "{\"label\":\"面向\",\"value\":\"" + $("#type_2_1_val").val() + "\"}," +
                    "{\"label\":\"课堂时间\",\"value\":\"" + $("#durations").val() + "\"}]";
            }
            //家庭端
            if (type == 1) {
                details = "[{\"label\":\"周次\",\"value\":\"" + $("#type_1_1_2_val").val()+ "\"}," +
                    "{\"label\":\"月龄\",\"value\":\"" + $("#ageLow").val() + "-" + $("#ageHigh").val() + "\"}," +
                    "{\"label\":\"面向\",\"value\":\"" + $("#type_2_2_val").val() + "\"}," +
                    "{\"label\":\"教材\",\"value\":\"" + $("#type_1_2_val").val() + "\"}," +
                    "{\"label\":\"课堂时间\",\"value\":\"" + $("#durations").val() + "\"}]";
            }
            //小群体课
            if (type == 2) {
                details = "[{\"label\":\"周次\",\"value\":\"" + $("#type_1_1_3_val").val() + "\"}," +
                    "{\"label\":\"教材\",\"value\":\"" + $("#type_1_2_val").val() + "\"}," +
                    "{\"label\":\"课堂时间\",\"value\":\"" + $("#durations").val() + "\"}]";
            }
            $("#details").val(details);
            $.operate.save(prefix + "/add", $('#form-course-add').serialize());
        }
    }

    $("input[name='lastUpdated']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $(function () {
        $('.summernote').summernote({
            lang: 'zh-CN',
            dialogsInBody: true,
            callbacks: {
                onChange: function (contents, $edittable) {
                    $("input[name='" + this.id + "']").val(contents);
                },
                onImageUpload: function (files) {
                    var obj = this;
                    var data = new FormData();
                    data.append("file", files[0]);
                    $.ajax({
                        type: "post",
                        url: ctx + "common/upload",
                        data: data,
                        cache: false,
                        contentType: false,
                        processData: false,
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == web_status.SUCCESS) {
                                $('#' + obj.id).summernote('insertImage', result.url);
                            } else {
                                $.modal.alertError(result.msg);
                            }
                        },
                        error: function (error) {
                            $.modal.alertWarning("图片上传失败。");
                        }
                    });
                }
            }
        });
    });

    $("#typeName").change(function () {
        var type = $("#typeName").val();
        //机构端
        if (type == 0) {
            $("#type_1_1").attr("style", "display:block;");//显示div
            $("#type_1_2").attr("style", "display:block;");//显示div
            $("#type_2_1").attr("style", "display:block;");//显示div

            $("#type_2_2").attr("style", "display:none;");//隐藏div
            //家庭端周次
            $("#type_1_1_2").attr("style", "display:none;");//隐藏div
            $("#type_1_1_3").attr("style", "display:none;");//隐藏div

        }
        //家庭端
        if (type == 1) {

            $("#type_1_1").attr("style", "display:none;");//隐藏div
            $("#type_2_1").attr("style", "display:none;");//隐藏div
            $("#type_1_1_3").attr("style", "display:none;");//隐藏div

            $("#type_2_2").attr("style", "display:block;");//隐藏div
            $("#indexInPackage_1").attr("style", "display:block;");//显示div
            $("#type_1_1_2").attr("style", "display:block;");//显示div

        }
        //小群体课
        if (type == 2) {
            $("#type_1_1").attr("style", "display:block;");//显示div
            $("#type_1_2").attr("style", "display:block;");//显示div
            $("#type_2_2").attr("style", "display:block;");//显示div
            $("#type_1_1_3").attr("style", "display:block;");//显示div

            $("#type_1_1").attr("style", "display:none;");//隐藏div
            $("#type_1_1_2").attr("style", "display:none;");//隐藏div
            $("#type_2_1").attr("style", "display:none;");//隐藏div
            $("#type_2_2").attr("style", "display:none;");//隐藏div

        }
    });
</script>
</body>
</html>